<template>
  <div class="orders">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card class="box-card">
      <el-input
        placeholder="请输入内容"
        v-model="pageinfo.query"
        class="input-with-select"
      >
        <el-button
          slot="append"
          icon="el-icon-search"
          @click="search"
        ></el-button>
      </el-input>

      <el-table :data="tableData" border stripe style="width: 100%">
        <el-table-column
          fixed
          type="index"
          width="40"
          label="#"
        ></el-table-column>
        <el-table-column prop="order_number" label="订单编号">
        </el-table-column>
        <el-table-column prop="order_price" label="订单价格"> </el-table-column>
        <el-table-column prop="pay_status" label="是否付款">
          <template slot-scope="scope">
            <el-tag
              :type="scope.row.is_send == '1' ? 'success' : 'danger'"
              >{{ scope.row.is_send == "1" ? "已付款" : "未付款" }}</el-tag
            >
          </template>
        </el-table-column>
        <el-table-column prop="is_send" label="是否发货"> </el-table-column>
        <el-table-column prop="create_time" label="下单时间">
          <template slot-scope="scope">
            {{ scope.row.create_time | filesTime }}
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="150">
          <template slot-scope="scope">
            <el-tooltip
              class="item"
              effect="dark"
              content="修改订单地址"
              placement="top"
            >
              <el-button
                type="primary"
                icon="el-icon-edit"
                size="small"
                @click="edit(scope.row)"
              ></el-button>
            </el-tooltip>
            <el-tooltip
              class="item"
              effect="dark"
              content="物流信息"
              placement="top"
            >
              <el-button
                type="success"
                icon="el-icon-location"
                size="small"
                @click="address(scope.row)"
              ></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page.sync="pageinfo.pagenum"
        :page-sizes="[1, 2, 5, 10]"
        :page-size.sync="pageinfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>

    <!-- 修改订单 -->
    <el-dialog title="修改地址" :visible.sync="dialogFormVisible">
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="省市区/县" prop="selectedOptions">
          <el-cascader
            v-model="ruleForm.selectedOptions"
            :options="options"
            @change="handleChange"
          ></el-cascader>
        </el-form-item>
        <el-form-item label="详细地址" prop="address">
          <el-input v-model="ruleForm.address"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogFormVisible = false"
          >确 定</el-button
        >
      </div>
    </el-dialog>

    <!-- 物流进度 -->
    <el-dialog title="物流进度" :visible.sync="dialogFormVisibles">
      <el-timeline :reverse="false">
        <el-timeline-item
          v-for="(activity, index) in activities"
          :key="index"
          :timestamp="activity.ftime"
        >
          {{ activity.context }}
        </el-timeline-item>
      </el-timeline>

      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisibles = false">取 消</el-button>
        <el-button type="primary" @click="okk">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { getOrders, getKuaidi } from "../utils/http";
import moment from "moment";
import { regionData, CodeToText } from "element-china-area-data";
export default {
  data() {
    return {
      pageinfo: {
        query: "", //搜索
        pagenum: 1, //当前页
        pagesize: 5, //每页条数
      },
      total: 0, //总页数
      tableData: [],
      dialogFormVisible: false,
      dialogFormVisibles: false,
      ruleForm: {
        selectedOptions: [],
        address: "",
      },
      rules: {
        address: [
          { required: true, message: "请输入详细地址", trigger: "blur" },
        ],
        selectedOptions: [
          { required: true, message: "请选择省市区", trigger: "change" },
        ],
      },
      activities: [],
      city: "",
      options: regionData,
    };
  },
  created() {
    this.getorders();
  },
  filters: {
    filesTime(value) {
      moment.locale("zh-ch");
      return moment(value).format("YYYY-MM-DD hh:mm:ss");
    },
  },
  methods: {
    //请求数据
    getorders() {
      getOrders(this.pageinfo).then((res) => {
        console.log(res);
        this.tableData = res.data.goods;
        this.total = res.data.total;
      });
    },
    //修改
    edit(row) {
      this.dialogFormVisible = true;
    },
    //三级联动
    handleChange() {
      var loc = "";
      for (let i = 0; i < this.ruleForm.selectedOptions.length; i++) {
        loc += CodeToText[this.ruleForm.selectedOptions[i]];
      }
      this.city = loc;
    },
    //发货进度
    address(row) {
      console.log(row);
      this.dialogFormVisibles = true;
      getKuaidi({ id: row.order_id }).then((res) => {
        // console.log(res);
        if (res.meta.status == 200) {
          this.activities = res.data;
          this.$message({
            type: "success",
            message: res.meta.msg,
          });
        } else {
          this.$message({
            type: "danger",
            message: res.meta.msg,
          });
        }
      });
    },
    //搜索
    search() {
      this.getorders();
    },
    //分页
    handleSizeChange(val) {
      this.pageinfo.pagesize = val;
      this.getorders();
    },
    //分页
    handleCurrentChange(val) {
      this.pageinfo.pagenum = val;
      this.getorders();
    },
    okk() {
      this.dialogFormVisible = false;
      this.ruleForm = {
        selectedOptions: [],
        address: "",
      };
    },
  },
};
</script>
<style lang="scss" scoped>
::v-deep .el-breadcrumb {
  margin-bottom: 20px;
}

::v-deep .input-with-select {
  width: 475px;
  margin-bottom: 20px;
}
.el-pagination {
  margin-top: 20px;
}
</style>